package com.zenchn.service.impl;

import com.zenchn.dao.TSysOauthClientMapper;
import com.zenchn.dynamic.TSysOauthClientDynamicMapper;
import com.zenchn.generic.GenericDao;
import com.zenchn.generic.GenericServiceImpl;
import com.zenchn.model.TSysOauthClient;
import com.zenchn.model.TSysOauthClientExample;
import com.zenchn.service.OauthClientService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
import static com.zenchn.dynamic.TSysOauthClientDynamicSqlSupport.*;
/**
 * 授权客户端 业务实现类
 *
 * @author dingxinfa
 * @date 2020-04-16
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class OauthClientServiceImpl extends GenericServiceImpl<TSysOauthClient, String, TSysOauthClientExample> implements OauthClientService {

	@Resource
	private TSysOauthClientMapper oauthClientMapper;


	@Resource
	private TSysOauthClientDynamicMapper oauthClientDynamicMapper;

	@Override
	public GenericDao<TSysOauthClient, String, TSysOauthClientExample> getDao() {
		return oauthClientMapper;
	}


	@Override
	public TSysOauthClient selectByClientSecret(String clientSecret) {

		TSysOauthClientExample example =new TSysOauthClientExample();
		
		example.createCriteria().andClientSecretEqualTo(clientSecret);
		
		List<TSysOauthClient> clientList=oauthClientMapper.selectByExample(example);
		
		if(clientList.size()>0){
			return clientList.get(0);
		}
		return null;
	}

	@Override
	public boolean existByClientId(String clientId) {
		if(oauthClientMapper.selectByPrimaryKey(clientId)!=null){
			return true;
		}
		return false;
	}

	@Override
	public boolean existByClientSecret(String clientId,String clientSecret) {
		TSysOauthClient client=oauthClientMapper.selectByPrimaryKey(clientId);
		
		if(client!=null){
			if(client.getClientSecret().equals(clientSecret)){
				return true;
			}
		}
		return false;
	}


}
